#
# export file: copy it to the build tree on every build invocation and add rule for installation
#
function(cm_export_file FILE DEST)
    if (NOT TARGET export-files)
        add_custom_target(export-files ALL COMMENT "Exporting files into build tree")
    endif (NOT TARGET export-files)

    get_filename_component(FILENAME "${FILE}" NAME)


  add_custom_command(TARGET export-files COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_SOURCE_DIR}/${FILE}" "${DEST}/${FILENAME}")
  install(FILES "${FILE}" DESTINATION "${DEST}")
endfunction(cm_export_file)

macro(configure_files srcDir destDir)
    message(STATUS "Configuring directory ${destDir}")
    make_directory(${destDir})

    file(GLOB templateFiles RELATIVE ${srcDir} ${srcDir}/*)
    foreach(templateFile ${templateFiles})
        set(srcTemplatePath ${srcDir}/${templateFile})
        if(NOT IS_DIRECTORY ${srcTemplatePath})
            message(STATUS "Configuring file ${templateFile}")
            configure_file(
                    ${srcTemplatePath}
                    ${destDir}/${templateFile}
                    @ONLY)
        endif(NOT IS_DIRECTORY ${srcTemplatePath})
    endforeach(templateFile)
endmacro(configure_files)

MACRO(COPY_FILE_IF_CHANGED in_file out_file target)

    #message("Copying file if changed: ${in_file} to: ${out_file} target: ${target}")
    IF (NOT TARGET ${target})
        message("Create Custom target:${target}")
        ADD_CUSTOM_TARGET(${target} ALL COMMENT "Exporting files into build tree ${target}")
    ENDIF (NOT TARGET ${target})

  add_custom_command(TARGET ${target} COMMAND ${CMAKE_COMMAND} -E copy_if_different "${in_file}" "${out_file}")
  install(FILES "${in_file}" DESTINATION "${out_file}")

ENDMACRO(COPY_FILE_IF_CHANGED)

MACRO(COPY_FILE_INTO_DIRECTORY_IF_CHANGED in_file out_dir target)
        GET_FILENAME_COMPONENT(file_name ${in_file} NAME)
        #message("in_file y1: ${in_file}")
        #message("file_name y2: ${file_name}")
        #message("out_dir y3: ${out_dir}")
        #message("target y4: ${target}")
        COPY_FILE_IF_CHANGED(${in_file} ${out_dir}/${file_name} ${target})
ENDMACRO(COPY_FILE_INTO_DIRECTORY_IF_CHANGED)

#Copies all the files from in_file_list into the out_dir.
# sub-trees are ignored (files are stored in same out_dir)
MACRO(COPY_FILES_INTO_DIRECTORY_IF_CHANGED in_file_list out_dir target)
    FOREACH(in_file ${in_file_list})
         #message("in_file x1: ${in_file}")
         #message("out_dir x2: ${out_dir}")
         #   message("target x2: ${target}")
        COPY_FILE_INTO_DIRECTORY_IF_CHANGED(${in_file} ${out_dir} ${target})
    ENDFOREACH(in_file)
ENDMACRO(COPY_FILES_INTO_DIRECTORY_IF_CHANGED)

macro(copy_files GLOBPAT DESTINATION target)
  file(GLOB COPY_FILES
    RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
    ${GLOBPAT})
  add_custom_target(${target} ALL
    COMMENT "Copying files: ${GLOBPAT}")

  foreach(FILENAME ${COPY_FILES})
    set(SRC "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}")
    set(DST "${DESTINATION}/${FILENAME}")

    add_custom_command(
      TARGET ${target}
      COMMAND ${CMAKE_COMMAND} -E copy ${SRC} ${DST}
      )
  endforeach(FILENAME)
endmacro(copy_files)

#Copy all files and directories in in_dir to out_dir.
# Subtrees remain intact.
MACRO(COPY_DIRECTORY_IF_CHANGED in_dir out_dir target)
    #message("Copying directory ${in_dir}")
    FILE(GLOB_RECURSE in_file_list ${in_dir}/*)
        FOREACH(in_file ${in_file_list})            
            STRING(REGEX REPLACE "${in_dir}" "${out_dir}" out_file ${in_file})
            COPY_FILE_IF_CHANGED(${in_file} ${out_file} ${target})

            #message("file 0: ${component_D}")
            #message("in_dir 1: ${in_dir}")
            #message("out_dir 2: ${out_dir}")
            #message("in_file 3: ${in_file}")
            #message("out_file 3: ${out_file}")

        ENDFOREACH(in_file)
ENDMACRO(COPY_DIRECTORY_IF_CHANGED)


FUNCTION(COPY_IF_DIFFERENT FILE DESTFILE TAR)
	IF (NOT TARGET ${TAR})
		ADD_CUSTOM_TARGET(${TAR} ALL COMMENT "Exporting files into build tree 1")
	ENDIF (NOT TARGET ${TAR})
	GET_FILENAME_COMPONENT(component_D ${DESTFILE} PATH)
	ADD_CUSTOM_COMMAND(TARGET ${TAR} COMMAND ${CMAKE_COMMAND} -E copy_if_different "${FILE}" "${DESTFILE}")
	INSTALL(FILES "${FILE}" DESTINATION "${component_D}")
ENDFUNCTION(COPY_IF_DIFFERENT)
